#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1005;

int n;
string addr[N];
unsigned data[N];
unsigned unet, umask;
string net, mask;
unsigned decode(string s) {
  unsigned a, b, c, d;
  sscanf(s.c_str(), "%u.%u.%u.%u", &a, &b, &c, &d);
  return (a << 24) | (b << 16) | (c << 8) | d;
}
string encode(unsigned x) {
  unsigned a[4];
  per(i, 3, 0) {
    a[i] = x & 255;
    x >>= 8;
  }
  char tmp[25] = {0};
  snprintf(tmp, 20, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
  return string(tmp);
}
bool check(int i) {
  unsigned k = ~((1u << i) - 1);
  unsigned net = data[1] & k;
  rep(j, 1, n) if ((data[j] & k) != net) return false;
  unet = net, umask = k;
  return true;
}
void solve() {
  rep(i, 1, n) data[i] = decode(addr[i]);
  rep(i, 0, 31) {
    if (check(i)) {
      net = encode(unet);
      mask = encode(umask);
      return;
    }
  }
  net = "0.0.0.0";
  mask = "0.0.0.0";
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("1.txt", "r", stdin);
  // freopen("2.txt", "w", stdout);
  while (cin >> n) {
    rep(i, 1, n) cin >> addr[i];
    solve();
    cout << net << endl << mask << endl;
  }
  return 0;
}